﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

namespace V5_EaspLibs.Core {
    /// <summary>
    /// 网站的安全,加密封装
    /// </summary>
    public class SecurityHelper {
        private static readonly SecurityHelper m_Instance = new SecurityHelper();
        /// <summary>
        /// 
        /// </summary>
        public static SecurityHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 
        /// </summary>
        public SecurityHelper() {

        }
        /// <summary>
        /// md5加密方式
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="code">16，32 位</param>
        /// <returns>加密后的字符串</returns>
        public string MD5(string str, int code) {
            if (code == 16) {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToLower().Substring(8, 16);
            }
            if (code == 32) {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToUpper();
            }
            return str;
        }
        /// <summary>
        /// Md5Sha1混合加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string MD5SHA1(string str) {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToUpper().Substring(0, 20) + FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToUpper().Substring(0, 20);
        }
        /// <summary>
        /// 安全SQL语句
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string SafeSQL(string str) {
            str = str.Trim();
            str = str.Replace("'", "''");
            str = str.Replace(";", "；");
            str = str.Replace("select", "Ｓelect");
            return str;
        }
    }
}
